package com.yanggeng.demo.beginnerguidedemo.utils;

import android.database.sqlite.SQLiteDatabase;

import com.yanggeng.demo.beginnerguidedemo.TestApplication;
import com.yanggeng.demo.beginnerguidedemo.bean.db.DaoMaster;
import com.yanggeng.demo.beginnerguidedemo.bean.db.DaoSession;

/**
 * GreenDao工具类
 * Created by yanggeng on 2017/7/21.
 */

public class GreenDaoUtils {

    private DaoMaster.DevOpenHelper helper;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;

    private static GreenDaoUtils greenDaoUtils;

    private GreenDaoUtils(){}

    public static GreenDaoUtils getInstance() {
        return SingleInstance.INSTANCE;
    }

    private static class SingleInstance {
        private static GreenDaoUtils INSTANCE = new GreenDaoUtils();
    }


    private void initGreenDao() {
        helper = new DaoMaster.DevOpenHelper(TestApplication.getApplication(), "CashBook.db", null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        if(daoMaster == null) {
            initGreenDao();
        }
        return daoSession;
    }

    public SQLiteDatabase getDb() {
        if(db == null) {
            initGreenDao();
        }
        return db;
    }
}
